package com.fieldchain.ex.fa100.utils.common;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * 状态栏相关工具类
 */
public class StatusBarUtils {

    private static StatusBarUtils statusBarUtils;
    private View statusBarView;

    public static StatusBarUtils getInstance() {
        if (null == statusBarUtils) {
            statusBarUtils = new StatusBarUtils();
        }
        return statusBarUtils;
    }

    //设置渐变色信号栏
    public void setDrawableTop(final Activity activity, final int bac) {
        activity.getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                initStatusBar(activity, bac);
            }
        });

    }

    //设置纯色信号栏
    public void setWindowStatusBarColor(Activity activity, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initStatusBar(Activity activity, int bac) {
        if (statusBarView != null) {
            statusBarView.setBackgroundResource(bac);
        } else {
            //android系统级的资源id得这么拿,不然拿不到
            int identifier = activity.getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = activity.getWindow().findViewById(identifier);
            statusBarView.setBackgroundResource(bac);
        }

    }


}
